<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("Writer.php");
//Includes
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("components4phpfull/jtpagecontrol.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class detailsEmission extends Page
{
       public $edtSearchJournal = null;
       public $btnSearchJournal = null;
       public $btnSearchEm = null;
       public $edtSearchEmission = null;
       public $lblPlateNum = null;
       public $Label49 = null;
       public $lblmodel = null;
       public $lblmake = null;
       public $lblyr = null;
       public $Label43 = null;
       public $Label38 = null;
       public $lblyear = null;
       public $Label48 = null;
       public $lblVeh = null;
       public $btnExport = null;
       public $btnExportIncident = null;
       public $JTIncidentHidden = null;
       public $JTEmissionHidden = null;
       public $JTJSEmission = null;
       public $imgBack = null;
       public $Image1 = null;
       public $JTEmission = null;
       public $JTJournal = null;
       public $JTSpecification = null;
       public $btnSearchSpec = null;
       public $imgSpecs = null;
       public $edtSearchSpec = null;
       public $hiddenId = null;
       public $imageAddSpecification = null;
       public $imageAddJournal = null;
       public $imageAddEmission = null;
       public $JTPageControl1 = null;
       public $JTSiteTheme1 = null;

       function btnExportJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/excel.png"
       <?php

       }

       function btnExportJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/excel2.png"
       <?php

       }

       function btnExportIncidentJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExportIncident'].src="image/excel.png"
       <?php

       }

       function btnExportIncidentJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExportIncident'].src="image/excel2.png"
       <?php

       }

       function btnExportIncidentClick($sender, $params)
       {
       $this->JTIncidentHidden->exportGridToXLSDownload('incident_report.xls');


       }


       function JTIncidentHiddenShow($sender, $params)
       {

       $result_vin = mysql_query("SELECT * FROM vehicle_tbl WHERE vehicle_id='" .$this->hiddenId->getValue(). "'");
       $row_selected = mysql_fetch_array($result_vin);


       $arr = array();

       $arr[] = array(
                        '1' => 'Vehicle Num:'.' '.$row_selected['VIN'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' '
                    );


       $arr[] = array(
                        '1' => 'Plate Num:'.' '.$row_selected['veh_num'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => 'Year:'.' '.$row_selected['year'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => 'Make:'.' '.$row_selected['manufacturer_name'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => 'Model:'.' '.$row_selected['model_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => 'Type/Style:'.' '.$row_selected['style_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' '
                    );



       $query1 = "select * from veh_incident_tbl where company_id='".$_SESSION['company_id']."' and vehicle_id='".$this->hiddenId->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());

       $arr[] = array(
                        '1' => 'INCIDENT REPORT',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => 'Entry Date',
                        '2' => 'Type',
                        '3' => 'Description',
                        '4' => 'Driver'
                        );

       while($row_journal = mysql_fetch_array($result))
       {
         $journal_id = $row_journal['incident_id'];
         $journal_edit = "<a href=veh_journal_edit.php?veh=".$this->hiddenId->getValue()."&journal=".$journal_id." title='edit journal'>".$row_journal['date']."</a>";
         $tools_journal = "<img onclick=js_deleteJournal('".$this->hiddenId->getValue()."','".$journal_id."'); src='image/delete.png' title='delete'>";
         //"<a href=veh_journal_delete.php?veh=".$this->hiddenId->getValue()."&journal=".$journal_id."><img src='image/delete_orange.jpg' title='delete' no border></a>";


          $arr[] = array(
                        '1' => $row_journal['date'],
                        '2' => $row_journal['type'],
                        '3' => $row_journal['description'],
                        '4' => $row_journal['driver']
                        );
       }

       $this->JTIncidentHidden->setCellData($arr);


       }

       function btnExportClick($sender, $params)
       {
       $this->JTEmissionHidden->exportGridToXLSDownload('emission_expense.xls');


       }

       function JTEmissionHiddenShow($sender, $params)
       {

       $result_vin = mysql_query("SELECT * FROM vehicle_tbl WHERE vehicle_id='" .$this->hiddenId->getValue(). "'");
       $row_selected = mysql_fetch_array($result_vin);


       $arr = array();

       $arr[] = array(
                        '1' => 'Vehicle Num:'.' '.$row_selected['VIN'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );


       $arr[] = array(
                        '1' => 'Plate Num:'.' '.$row_selected['veh_num'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );

       $arr[] = array(
                        '1' => 'Year:'.' '.$row_selected['year'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );

       $arr[] = array(
                        '1' => 'Make:'.' '.$row_selected['manufacturer_name'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );

       $arr[] = array(
                        '1' => 'Model:'.' '.$row_selected['model_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );

       $arr[] = array(
                        '1' => 'Type/Style:'.' '.$row_selected['style_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );

       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );


       $query0 = "select * from veh_emission_tbl where vehicle_id='".$this->hiddenId->getValue()."'and (inspection_date like '%".$this->edtSearchEmission->Text."%'
        or validity_date like '%".$this->edtSearchEmission->Text."%'
        or inspector like '%".$this->edtSearchEmission->Text."%') order by emission_id ASC";
       $result0 = mysql_query($query0)or die(mysql_error());


       $arr[] = array(
                        '1' => 'EMISSION REPORT',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' '
                    );


       $arr[] = array(
                        '1' => 'Test Date',
                        '2' => 'Valid Thru',
                        '3' => 'Inspector',
                        '4' => 'Account',
                        '5' => 'Expense'
                        );


       while($row = mysql_fetch_array($result0))
       {
         $emission = $row['emission_id'];
         $test_date = "<a href=veh_emission_edit.php?veh=".$this->hiddenId->getValue()."&emission=".$emission." title='edit emission'>".$row['inspection_date']."</a>";
         $tools_emission = "<img onclick=js_deleteEmission('".$this->hiddenId->getValue()."','".$emission."'); src='image/delete_orange.jpg' title='delete'>";
         //<a href=veh_emission_delete.php?veh=".$this->hiddenId->getValue()."&emission=".$emission."><img src='image/delete_orange.jpg' title='delete' no border></a>";

         $arr[] = array(
                        '1' => $row['inspection_date'],
                        '2' => $row['validity_date'],
                        '3' => $row['inspector'],
                        '4' => $row['account'],
                        '5' => $row['test_fee']
                        );

        }
        $total = mysql_query("SELECT SUM(test_fee) as sum_fee FROM veh_emission_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
        $row_total = mysql_fetch_array($total);

        $arr[] = array(
                        '1' => TOTAL,
                        '5' => $row_total['sum_fee'],
                        );





        $this->JTEmissionHidden->setCellData($arr);


       }



       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function imageAddSpecificationJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddSpecification'].src="image/add_new.png"
       <?php

       }

       function imageAddSpecificationJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddSpecification'].src="image/add_new_blue.png"
       <?php

       }

       function imageAddEmissionJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddEmission'].src="image/add_new.png"
       <?php

       }

       function imageAddEmissionJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddEmission'].src="image/add_new_blue.png"
       <?php

       }

       function imageAddJournalJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddJournal'].src="image/add_new.png"
       <?php

       }

       function imageAddJournalJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imageAddJournal'].src="image/add_new_blue.png"
       <?php

       }

       function btnSearchJournalClick($sender, $params)
       {
       redirect('details_emission.php?veh='.$this->hiddenId->getValue().'&layer=1&search_journal='.$this->edtSearchJournal->getText());
       }

       function btnSearchSpecClick($sender, $params)
       {
       redirect('details_emission.php?veh='.$this->hiddenId->getValue().'&layer=2&search_spec='.$this->edtSearchSpec->getText());
       }

       function edtSearchSpecJSClick($sender, $params)
       {
       ?>
       //Add your javascript code here
       document.getElementById('edtSearchSpec').value = '';
       <?php

       }

       function edtSearchJournalJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edtSearchJournal').value = '';
       <?php

       }

       function edtSearchEmissionJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edtSearchEmission').value = '';
       <?php

       }


       function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }


       function detailsEmissionCreate($sender, $params)
       {
        if($_SESSION['isAdmin']!=1)
        {
         $this->imageAddEmission->setVisible(false);
         $this->imageAddJournal->setVisible(false);
         $this->imageAddSpecification->setVisible(false);
         $this->btnExport->Left = 928;
         $this->btnExportIncident->Left = 928;
        }

        $_SESSION['company_id'];
        $id = $_GET['veh'];
        if($id!=null || $id!='')
        $this->hiddenId->setValue($id);

        $this->JTPageControl1->TabIndex = $_GET['layer'];

        //$this->JTEmission_header_0_caption->align="center";

        $this->imgBack->setLink('vehicle_list.php?company_id='.$_SESSION['company_id']);
        $this->imageAddEmission->setLink('veh_emission_create.php?veh='.$this->hiddenId->getValue());
        $this->imageAddSpecification->setLink('veh_specification_create.php?veh='.$this->hiddenId->getValue());
        $this->imageAddJournal->setLink('veh_incident_create.php?veh='.$this->hiddenId->getValue());

        $result_vin = mysql_query("SELECT * FROM vehicle_tbl WHERE vehicle_id='" .$this->hiddenId->getValue(). "'");
        $row_selected = mysql_fetch_array($result_vin);
        $label = $row_selected['VIN'];
        $this->lblVeh->Caption = $label;
        $this->lblyr->Caption = $row_selected['year'];
        $this->lblmake->Caption = $row_selected['manufacturer_name'];
        $this->lblmodel->Caption = $row_selected['model_name'];
        $this->lblPlateNum->Caption = $row_selected['veh_num'];

       }

       function JTSpecificationShow($sender, $params)
       {
       $this->edtSearchSpec->setText($_GET['search_spec']);
       $query2 = "select * from veh_specification_tbl where vehicle_id='".$this->hiddenId->getValue()."' and (description like '%".$this->edtSearchSpec->Text."%'
        or quantity like '%".$this->edtSearchSpec->Text."%') order by specification_id ASC";
       $result2 = mysql_query($query2)or die(mysql_error());

       $arr = array();
       while($row_spec = mysql_fetch_array($result2))
       {

       $spec_id = $row_spec['specification_id'];
       $spec_edit = "<a href=veh_specification_edit.php?veh=".$this->hiddenId->getValue()."&specification=".$spec_id." title='edit specification'>".$row_spec['description']."</a>";
       $tools_spec = "<img onclick=js_deleteSpec('".$this->hiddenId->getValue()."','".$spec_id."'); src='image/delete.png' title='delete'>";
       //"<a href=veh_specification_delete.php?veh=".$this->hiddenId->getValue()."&specification=".$spec_id."><img src='image/delete_orange.jpg' title='delete' no border></a>";

       if($_SESSION['isAdmin']==1)
       {
       $arr[] = array(
                        'Description' => $spec_edit,
                        'Value/Qty' => $row_spec['quantity'],
                        'Tools' => $tools_spec
                        );
       }

       if($_SESSION['isAdmin']!=1)
       {
       $arr[] = array(
                        'Description' => $row_spec['description'],
                        'Value/Qty' => $row_spec['quantity']
                        );
       }

       }

       $this->JTSpecification->setCellData($arr);


       }

       function JTJournalShow($sender, $params)
       {
       $this->edtSearchJournal->setText($_GET['search_journal']);
       $query1 = "select * from veh_incident_tbl where company_id='".$_SESSION['company_id']."' and vehicle_id='".$this->hiddenId->getValue()."' and (date like '%".$this->edtSearchJournal->Text."%'
        or type like '%".$this->edtSearchJournal->Text."%'
        or description like '%".$this->edtSearchJournal->Text."%'
        or driver like '%".$this->edtSearchJournal->Text."%'
        or cost like '%".$this->edtSearchJournal->Text."%'
        or ref_num like '%".$this->edtSearchJournal->Text."%'
        or comments like '%".$this->edtSearchJournal->Text."%') order by incident_id ASC";
       $result = mysql_query($query1)or die(mysql_error());

       $arr = array();
       while($row_journal = mysql_fetch_array($result))
       {
         $journal_id = $row_journal['incident_id'];
         $journal_edit = "<a href=veh_incident_edit.php?veh=".$this->hiddenId->getValue()."&journal=".$journal_id." title='edit journal'>".$row_journal['date']."</a>";
         $tools_journal = "<img onclick=js_deleteJournal('".$this->hiddenId->getValue()."','".$journal_id."'); src='image/delete.png' title='delete'>";
         //"<a href=veh_journal_delete.php?veh=".$this->hiddenId->getValue()."&journal=".$journal_id."><img src='image/delete_orange.jpg' title='delete' no border></a>";

         if($_SESSION['isAdmin']==1)
         {
         $arr[] = array(
                        'Incident Date' => $journal_edit,
                        'Description' => $row_journal['description'],
                        //'Type' => $row_journal['type'],
                        'Driver' => $row_journal['driver'],
                        'Tools' => $tools_journal
                        );
         }
         if($_SESSION['isAdmin']!=1)
         {
          $arr[] = array(
                        'Incident Date' => $row_journal['date'],
                        'Description' => $row_journal['description'],
                        //'Type' => $row_journal['type'],
                        'Driver' => $row_journal['driver']
                        );
         }
       }

       $this->JTJournal->setCellData($arr);


       }

       function JTEmissionShow($sender, $params)
       {
       $query0 = "select * from veh_emission_tbl where vehicle_id='".$this->hiddenId->getValue()."'and (inspection_date like '%".$this->edtSearchEmission->Text."%'
        or validity_date like '%".$this->edtSearchEmission->Text."%'
        or inspector like '%".$this->edtSearchEmission->Text."%') order by emission_id ASC";
       $result0 = mysql_query($query0)or die(mysql_error());

       $arr = array();
       while($row = mysql_fetch_array($result0))
       {
         $emission = $row['emission_id'];
         $test_date = "<a href=veh_emission_edit.php?veh=".$this->hiddenId->getValue()."&emission=".$emission." title='edit emission'>".$row['inspection_date']."</a>";
         $tools_emission = "<img onclick=js_deleteEmission('".$this->hiddenId->getValue()."','".$emission."'); src='image/delete.png' title='delete'>";
         //<a href=veh_emission_delete.php?veh=".$this->hiddenId->getValue()."&emission=".$emission."><img src='image/delete_orange.jpg' title='delete' no border></a>";

         $tf = $row['test_fee'];
         $test_f = $this->commafy($tf);


         if($_SESSION['isAdmin']==1)
         {
         $arr[] = array(
                        'Test Date' => $test_date,
                        'Valid Thru' => $row['validity_date'],
                        'Inspector' => $row['inspector'],
                        'Account' => $row['account'],
                        'Expense' => "<p style align='right'>".$test_f."</p>",
                        'Tools' => $tools_emission
                        );
         }
         if($_SESSION['isAdmin']!=1)
         {
         $arr[] = array(
                        'Test Date' => $row['inspection_date'],
                        'Valid Thru' => $row['validity_date'],
                        'Inspector' => $row['inspector'],
                        'Account' => $row['account'],
                        'Expense' => "<p style align='right'>".$test_f."</p>"
                        );
         }

        }
        $total = mysql_query("SELECT SUM(test_fee) as sum_fee FROM veh_emission_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
        $row_total = mysql_fetch_array($total);

        $tot = $row_total['sum_fee'];
        $sum_tot = $this->commafy($tot);

        $arr[] = array(
                        'Test Date' => TOTAL,
                        'Expense' => "<p style align='right'>".$sum_tot."</p>"
                        );





        $this->JTEmission->setCellData($arr);


       }

}

global $application;

global $detailsEmission;

//Creates the form
$detailsEmission=new detailsEmission($application);

//Read from resource file
$detailsEmission->loadResource(__FILE__);

//Shows the form
$detailsEmission->show();

?>